<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>actionhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_actionhandler.js.html">actionhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file contains a class to provide a unified mechanism for
<a name="line17"></a> * CLICK and enter KEYDOWN events. This provides better accessibility by
<a name="line18"></a> * providing the given functionality to a keyboard user which is otherwise
<a name="line19"></a> * would be available only via a mouse click.
<a name="line20"></a> *
<a name="line21"></a> * If there is an existing CLICK listener or planning to be added as below -
<a name="line22"></a> *
<a name="line23"></a> * &lt;code&gt;this.eventHandler_.listen(el, CLICK, this.onClick_);&lt;code&gt;
<a name="line24"></a> *
<a name="line25"></a> * it can be replaced with an ACTION listener as follows:
<a name="line26"></a> *
<a name="line27"></a> * &lt;code&gt;this.eventHandler_.listen(
<a name="line28"></a> *    new goog.events.ActionHandler(el),
<a name="line29"></a> *    ACTION,
<a name="line30"></a> *    this.onAction_);&lt;code&gt;
<a name="line31"></a> *
<a name="line32"></a> */
<a name="line33"></a>
<a name="line34"></a>goog.provide(&#39;goog.events.ActionEvent&#39;);
<a name="line35"></a>goog.provide(&#39;goog.events.ActionHandler&#39;);
<a name="line36"></a>goog.provide(&#39;goog.events.ActionHandler.EventType&#39;);
<a name="line37"></a>goog.provide(&#39;goog.events.BeforeActionEvent&#39;);
<a name="line38"></a>
<a name="line39"></a>goog.require(&#39;goog.events&#39;);
<a name="line40"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line41"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line42"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line43"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line44"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * A wrapper around an element that you want to listen to ACTION events on.
<a name="line50"></a> * @param {Element|Document} element The element or document to listen on.
<a name="line51"></a> * @constructor
<a name="line52"></a> * @extends {goog.events.EventTarget}
<a name="line53"></a> */
<a name="line54"></a>goog.events.ActionHandler = function(element) {
<a name="line55"></a>  goog.events.EventTarget.call(this);
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * This is the element that we will listen to events on.
<a name="line59"></a>   * @type {Element|Document}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.element_ = element;
<a name="line63"></a>
<a name="line64"></a>  goog.events.listen(element, goog.events.ActionHandler.KEY_EVENT_TYPE_,
<a name="line65"></a>      this.handleKeyDown_, false, this);
<a name="line66"></a>  goog.events.listen(element, goog.events.EventType.CLICK,
<a name="line67"></a>      this.handleClick_, false, this);
<a name="line68"></a>};
<a name="line69"></a>goog.inherits(goog.events.ActionHandler, goog.events.EventTarget);
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Enum type for the events fired by the action handler
<a name="line74"></a> * @enum {string}
<a name="line75"></a> */
<a name="line76"></a>goog.events.ActionHandler.EventType = {
<a name="line77"></a>  ACTION: &#39;action&#39;,
<a name="line78"></a>  BEFOREACTION: &#39;beforeaction&#39;
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Key event type to listen for.
<a name="line84"></a> * @type {string}
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.events.ActionHandler.KEY_EVENT_TYPE_ = goog.userAgent.GECKO ?
<a name="line88"></a>    goog.events.EventType.KEYPRESS :
<a name="line89"></a>    goog.events.EventType.KEYDOWN;
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Handles key press events.
<a name="line94"></a> * @param {!goog.events.BrowserEvent} e The key press event.
<a name="line95"></a> * @private
<a name="line96"></a> */
<a name="line97"></a>goog.events.ActionHandler.prototype.handleKeyDown_ = function(e) {
<a name="line98"></a>  if (e.keyCode == goog.events.KeyCodes.ENTER ||
<a name="line99"></a>      goog.userAgent.WEBKIT &amp;&amp; e.keyCode == goog.events.KeyCodes.MAC_ENTER) {
<a name="line100"></a>    this.dispatchEvents_(e);
<a name="line101"></a>  }
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Handles mouse events.
<a name="line107"></a> * @param {!goog.events.BrowserEvent} e The click event.
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.events.ActionHandler.prototype.handleClick_ = function(e) {
<a name="line111"></a>  this.dispatchEvents_(e);
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Dispatches BeforeAction and Action events to the element
<a name="line117"></a> * @param {!goog.events.BrowserEvent} e The event causing dispatches.
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.events.ActionHandler.prototype.dispatchEvents_ = function(e) {
<a name="line121"></a>  var beforeActionEvent = new goog.events.BeforeActionEvent(e);
<a name="line122"></a>
<a name="line123"></a>  // Allow application specific logic here before the ACTION event.
<a name="line124"></a>  // For example, Gmail uses this event to restore keyboard focus
<a name="line125"></a>  if (!this.dispatchEvent(beforeActionEvent)) {
<a name="line126"></a>    // If the listener swallowed the BEFOREACTION event, don&#39;t dispatch the
<a name="line127"></a>    // ACTION event.
<a name="line128"></a>    return;
<a name="line129"></a>  }
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>  // Wrap up original event and send it off
<a name="line133"></a>  var actionEvent = new goog.events.ActionEvent(e);
<a name="line134"></a>  try {
<a name="line135"></a>    this.dispatchEvent(actionEvent);
<a name="line136"></a>  } finally {
<a name="line137"></a>    // Stop propagating the event
<a name="line138"></a>    e.stopPropagation();
<a name="line139"></a>  }
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/** @override */
<a name="line144"></a>goog.events.ActionHandler.prototype.disposeInternal = function() {
<a name="line145"></a>  goog.events.ActionHandler.superClass_.disposeInternal.call(this);
<a name="line146"></a>  goog.events.unlisten(this.element_, goog.events.ActionHandler.KEY_EVENT_TYPE_,
<a name="line147"></a>      this.handleKeyDown_, false, this);
<a name="line148"></a>  goog.events.unlisten(this.element_, goog.events.EventType.CLICK,
<a name="line149"></a>      this.handleClick_, false, this);
<a name="line150"></a>  delete this.element_;
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * This class is used for the goog.events.ActionHandler.EventType.ACTION event.
<a name="line157"></a> * @param {!goog.events.BrowserEvent} browserEvent Browser event object.
<a name="line158"></a> * @constructor
<a name="line159"></a> * @extends {goog.events.BrowserEvent}
<a name="line160"></a> */
<a name="line161"></a>goog.events.ActionEvent = function(browserEvent) {
<a name="line162"></a>  goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());
<a name="line163"></a>  this.type = goog.events.ActionHandler.EventType.ACTION;
<a name="line164"></a>};
<a name="line165"></a>goog.inherits(goog.events.ActionEvent, goog.events.BrowserEvent);
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * This class is used for the goog.events.ActionHandler.EventType.BEFOREACTION
<a name="line171"></a> * event. BEFOREACTION gives a chance to the application so the keyboard focus
<a name="line172"></a> * can be restored back, if required.
<a name="line173"></a> * @param {!goog.events.BrowserEvent} browserEvent Browser event object.
<a name="line174"></a> * @constructor
<a name="line175"></a> * @extends {goog.events.BrowserEvent}
<a name="line176"></a> */
<a name="line177"></a>goog.events.BeforeActionEvent = function(browserEvent) {
<a name="line178"></a>  goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());
<a name="line179"></a>  this.type = goog.events.ActionHandler.EventType.BEFOREACTION;
<a name="line180"></a>};
<a name="line181"></a>goog.inherits(goog.events.BeforeActionEvent, goog.events.BrowserEvent);
</pre>


</body>
</html>
